/**
 * Created with IntelliJ IDEA.
 * User: AD
 * Date: 08/01/13
 * Time: 15:04
 * To change this template use File | Settings | File Templates.
 */

function addStudent() {
    var firstName = document.getElementById('studentFirstName').value;
    var lastName = document.getElementById('studentLastName').value;
    var http = new XMLHttpRequest();
    var url = "/webapp/rest/student";
    var params = "{\"firstName\":\"" + firstName + "\",\"lastName\":\"" + lastName + "\"}";
    http.open("POST", url, false);
    http.setRequestHeader("Content-type", "application/json");
    http.send(params);
    getAllLectures();
    getAllStudents();
}

function addLecture() {
    var name = document.getElementById('lectureName').value;
    var http = new XMLHttpRequest();
    var url = "/webapp/rest/lecture";
    var params = "{\"name\":\"" + name + "\"}";
    http.open("POST", url, false);
    http.setRequestHeader("Content-type", "application/json");
    http.send(params);
    getAllLectures();
    getAllStudents();
}

function addStudentToLecture() {
    var lectureId = document.getElementById('lectureId').value;
    var studentId = document.getElementById('studentId').value;
    var http = new XMLHttpRequest();
    var url = "/webapp/rest/lecture/" + lectureId + "/";
    var params = "{\"id\":\"" + studentId + "\"}";
    http.open("POST", url, false);
    http.setRequestHeader("Content-type", "application/json");
    http.send(params);
    getAllLectures();
    getAllStudents();
}

function removeStudentFromLecture() {
    var lectureId = document.getElementById('lectureIdToUnenrollStudent').value;
    var studentId = document.getElementById('studentIdToRemoveFromLecture').value;
    var http = new XMLHttpRequest();
    var url = "/webapp/rest/lecture/" + lectureId + "/" + studentId;
    http.open("DELETE", url, false);
    http.setRequestHeader("Content-type", "application/json");
    http.send();
    getAllLectures();
    getAllStudents();
}

function removeLecture() {
    var lectureId = document.getElementById('lectureIdToRemove').value;
    var http = new XMLHttpRequest();
    var url = "/webapp/rest/lecture/" + lectureId;
    http.open("DELETE", url, false);
    http.setRequestHeader("Content-type", "application/json");
    http.send();
    getAllLectures();
    getAllStudents();
}

function removeStudent() {
    var studentId = document.getElementById('studentIdToRemove').value;
    var http = new XMLHttpRequest();
    var url = "/webapp/rest/student/" + studentId;
    http.open("DELETE", url, false);
    http.setRequestHeader("Content-type", "application/json");
    http.send();
    getAllLectures();
    getAllStudents();
}
function refresh() {
    getAllLectures();
    getAllStudents();
}

function getAllLectures() {
    var req = new XMLHttpRequest();
    req.open('GET', 'http://localhost:8080/webapp/rest/lecture', false);
    req.send();
    document.getElementById('currentLectures').innerHTML = req.responseText;
}

function getAllStudents() {
    var req = new XMLHttpRequest();
    req.open('GET', 'http://localhost:8080/webapp/rest/student', false);
    req.send();
    document.getElementById('currentStudents').innerHTML = req.responseText;
}


